<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category Zeed
 * @package Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author Zeed Team (http://blog.zeed.com.cn)
 * @since 2010-12-6
 * @version SVN: $Id$
 */

class VodController extends WorkbenchAdminAbstract
{
    /**
     * 点播视频处理
     */
    public function index()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        $card_id = $this->input->query('card_id', 0);
        
        /* ajax 加载数据 */
        if ($this->input->isPOST()) {
            /* 获取文件信息 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (! empty($attachment_info)) {
                $filepath = Support_Image_Url::getImageUrl($attachment_info[0]['filepath']);
            }
            
            /* 获取相关文件列表 */
            $attachment_list = array();
            if ($card_id) {
                $card_info = Archive_Model_Card::instance()->fetchByPK($card_id);
                if (! empty($card_info) && $card_info[0]['attachments_video']) {
                    $attachments_video = explode(',', $card_info[0]['attachments_video']);
                    $attachment_list = Attachment_Model_Content::instance()->fetchByPK($attachments_video, 
                            array('_id', 'filepath', 'title'));
                    foreach ($attachment_list as $k => &$v_al) {
                        $v_al['filepath'] = Support_Image_Url::getImageUrl($v_al['filepath']);
                    }
                }
            }
            
            /* 处理添加时的编辑情况 */
            if (empty($attachment_list) && ! empty($attachment_info)) {
                $attachment_list[] = array(
                        '_id' => $attachment_info[0]['_id'],
                        'filepath' => $filepath,
                        'title' => $attachment_info[0]['title']
                );
            }
            
            /* 获取已打点内容 */
            $where_dot = array('attachment_id' => $_id, 'userid' => $this->_userid);
            $dots = Attachment_Model_Content_Dot::instance()->fetchByWhere($where_dot);
            
            /* 获取截短历史 */
            $where_clipvideo = array('pid' => $_id, 'mediatype' => 'video', 'status' => 1, 'userid' => $this->_userid);
            $clip_videos = Attachment_Model_Content::instance()->fetchByWhere($where_clipvideo);
            if (! empty($clip_videos)) {
                foreach ($clip_videos as $k => &$v_v) {
                    /* 处理截短视频地址 */
                    $v_v['filepath'] = Support_Image_Url::getImageUrl($v_v['filepath']);
                    
                    /* 处理扩展信息 */
                    if (! empty($v_v['extra'])) {
                        $extra_v = json_decode($v_v['extra'], true);
                        $v_v['time_start'] = Util_Timer::timeFormat(intval($extra_v['time_start']));
                        $v_v['time_end'] = Util_Timer::timeFormat(intval($extra_v['time_end']));
                        if (substr($v_v['time_start'], 0, 3) == '00:') {
                            $v_v['time_start'] = substr($v_v['time_start'], 3);
                        }
                        if (substr($v_v['time_end'], 0, 3) == '00:') {
                            $v_v['time_end'] = substr($v_v['time_end'], 3);
                        }
                    }
                }
            }
            
            /* 获取截图历史 */
            $where_clipimage = array('pid' => $_id, 'mediatype' => 'image', 'status' => 1, 'userid' => $this->_userid);
            $clip_images = Attachment_Model_Content::instance()->fetchByWhere($where_clipimage);
            if (! empty($clip_images)) {
                foreach ($clip_images as $k => &$v_i) {
                    /* 处理截图地址 */
                    $v_i['filepath'] = Support_Image_Url::getImageUrl($v_i['filepath']);
                    
                    /* 处理扩展信息 */
                    if (! empty($v_i['extra'])) {
                        $extra_i = json_decode($v_i['extra'], true);
                        $v_i['gmt'] = isset($extra_i['gmt']) ? $extra_i['gmt'] : '';
                    }
                }
            }
            
            $data = array(
                    '_id' => $_id,
                    'card_id' => $card_id,
                    'filepath' => isset($filepath) ? $filepath : '',
                    'dots' => ! empty($dots) ? $dots : array(),
                    'clip_videos' => ! empty($clip_videos) ? $clip_videos : array(),
                    'clip_images' => ! empty($clip_images) ? $clip_images : array(),
                    'attachment_list' => $attachment_list
            );
            
            $this->setData('data', $data);
            return self::RS_SUCCESS;
        }
        
        $this->addResult(self::RS_SUCCESS, 'php', 'vod.index');
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 截短
     */
    public function clipVideo()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        $time_start = $this->input->query('time_start', 0);
        $time_end = $this->input->query('time_end');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id || ! $time_end) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查文件是否存在 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (empty($attachment_info)) {
                throw new Zeed_Exception('查无此文件');
            }
            $attachment_info = $attachment_info[0];
            
            /* 执行截短 */
            $result = Trend_Attachment_ClipVideo::run($attachment_info['filepath'], $time_start, $time_end);
            if ($result['status'] !== 0) {
                throw new Zeed_Exception($result['error']);
            }
            
            /* 自动抽取封面 */
            $cover = '';
            $result_cover = Trend_Attachment_ClipVideoCover::run($result['data']['filepath'], 1);
            if ($result_cover['status'] == 0) {
                $cover = $result_cover['data']['filepath'];
            }

            /* 截短信息入库 */
            $timenow = Util_Timer::getMicrotime();
            $abspath = ZEED_PATH_UPLOAD . $result['data']['filepath'];
            $extra = array(
                    'duration' => $time_end - $time_start,
                    'time_start' => $time_start,
                    'time_end' => $time_end,
                    'cover' => $cover
            );
            $set_clip = array(
                    'pid' => $_id,
                    'userid' => $this->_userid,
                    'title' => $this->renameTitle($attachment_info['title'], 'clip_video'),
                    'mediatype' => 'video',
                    'mimetype' => 'video/mp4',
                    'suffix' => 'mp4',
                    'image_width' => 0,
                    'image_height' => 0,
                    'filepath' => $result['data']['filepath'],
                    'filesize' => filesize($abspath),
                    'hashcode' => md5_file($abspath),
                    'folder_hid' => '',
                    'order_id' => '',
                    'source_code' => 2,
                    'extra' => json_encode($extra),
                    'status' => 1,
                    'ctime' => $timenow,
                    'mtime' => $timenow
            );
            $clip_id = Attachment_Model_Content::instance()->addForEntity($set_clip);
            
            /* @todo 新增一条日志记录 */
            
            $data = array(
                    '_id' => $clip_id,
                    'filepath' => Support_Image_Url::getImageUrl($result['data']['filepath'])
            );
            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('截短失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 截短 - 删除
     */
    public function clipVideoDelete()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查文件是否存在 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (empty($attachment_info)) {
                throw new Zeed_Exception('查无此文件');
            }
            $attachment_info = $attachment_info[0];
            
            /* 执行删除 */
            $set = array(
                    'status' => -1,
                    'mtime' => Util_Timer::getMicrotime()
            );
            Attachment_Model_Content::instance()->updateForEntity($set, $_id);
            
            /* @todo 新增一条日志记录 */
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('删除失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 截图
     */
    public function clipImage()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        $gmt = $this->input->query('gmt');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id || ! $gmt) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查文件是否存在 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (empty($attachment_info)) {
                throw new Zeed_Exception('查无此文件');
            }
            $attachment_info = $attachment_info[0];
            
            /* 执行截图 */
            $result = Trend_Attachment_ClipImage::run($attachment_info['filepath'], $gmt);
            if ($result['status'] !== 0) {
                throw new Zeed_Exception($result['error']);
            }
            
            /* 截图信息入库 */
            $timenow = Util_Timer::getMicrotime();
            $abspath = ZEED_PATH_UPLOAD . $result['data']['filepath'];
            $image_info = getimagesize($abspath);
            $extra = array('gmt' => $gmt);
            $set_clip = array(
                    'pid' => $_id,
                    'userid' => $this->_userid,
                    'title' => $this->renameTitle($attachment_info['title'], 'clip_video_image'),
                    'mediatype' => 'image',
                    'mimetype' => 'image/jpeg',
                    'suffix' => 'jpg',
                    'image_width' => $image_info[0],
                    'image_height' => $image_info[1],
                    'filepath' => $result['data']['filepath'],
                    'filesize' => filesize($abspath),
                    'hashcode' => md5_file($abspath),
                    'folder_hid' => '',
                    'order_id' => '',
                    'source_code' => 2,
                    'extra' => json_encode($extra),
                    'status' => 1,
                    'ctime' => $timenow,
                    'mtime' => $timenow
            );
            $clip_id = Attachment_Model_Content::instance()->addForEntity($set_clip);
            
            /* @todo 新增一条日志记录 */
            
            $data = array(
                    '_id' => $clip_id,
                    'filepath' => Support_Image_Url::getImageUrl($result['data']['filepath'])
            );
            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('截图失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 截图 - 删除
     */
    public function clipImageDelete()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查文件是否存在 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (empty($attachment_info)) {
                throw new Zeed_Exception('查无此文件');
            }
            $attachment_info = $attachment_info[0];
            
            /* 执行删除 */
            $set = array(
                    'status' => -1,
                    'mtime' => Util_Timer::getMicrotime()
            );
            Attachment_Model_Content::instance()->updateForEntity($set, $_id);
            
            /* @todo 新增一条日志记录 */
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('删除失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 打点 - 添加
     */
    public function dot()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        $gmt = $this->input->query('gmt');
        $content = $this->input->query('content');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id || ! $gmt || ! $content) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查文件是否存在 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (empty($attachment_info)) {
                throw new Zeed_Exception('查无此文件');
            }
            $attachment_info = $attachment_info[0];
            
            /* 查询文件所属行动信息 */
            $where_card['$or'] = array(
                    array('attachments_audio' => new MongoRegex("/$_id/i")),
                    array('attachments_video' => new MongoRegex("/$_id/i")),
            );
            $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
            
            /* 查询行动所属案件信息 */
            if (! empty($card_info)) {
                $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $card_info[0]['sn']);
            }
            
            /* 执行添加 */
            $timenow = Util_Timer::getMicrotime();
            $set_add = array(
                    'mediatype' => $attachment_info['mediatype'],
                    'attachment_id' => $_id,
                    'card_id' => ! empty($card_info) ? $card_info[0]['_id'] : '',
                    'sn' => ! empty($card_info) ? $card_info[0]['sn'] : '',
                    'category_name' => ! empty($archive_info) ? $archive_info[0]['category_name'] : '',
                    'department_name' => ! empty($archive_info) ? $archive_info[0]['department_name'] : '',
                    'ctime_archive' => ! empty($archive_info) ? $archive_info[0]['ctime'] : '',
                    'userid' => $this->_userid,
                    'gmt' => $gmt,
                    'content' => $content,
                    'status' => 1,
                    'ctime' => $timenow,
                    'mtime' => $timenow
            );
            Attachment_Model_Content_Dot::instance()->addForEntity($set_add);
            
            /* @todo 新增一条日志记录 */
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('打点失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 打点 - 编辑
     */
    public function dotEdit()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = $this->input->query('_id');
        $gmt = $this->input->query('gmt');
        $content = $this->input->query('content');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id || ! $gmt || ! $content) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查打点信息是否存在 */
            $where_dot = array('attachment_id' => $_id, 'gmt' => $gmt);
            $dot_info = Attachment_Model_Content_Dot::instance()->fetchByWhere($where_dot);
            if (empty($dot_info)) {
                throw new Zeed_Exception('查无此打点信息');
            }
            $dot_info = $dot_info[0];
            
            /* 执行更新 */
            $set_update = array(
                    'content' => $content,
                    'mtime' => Util_Timer::getMicrotime()
            );
            Attachment_Model_Content_Dot::instance()->updateForEntity($set_update, $dot_info['_id']);
            
            /* @todo 新增一条日志记录 */
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('编辑失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 打点 - 删除
     */
    public function dotDelete()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $_id = trim($this->input->query('_id'));
        $gmt = $this->input->query('gmt');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $_id || ! $gmt) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 检查文件是否存在 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($_id);
            if (empty($attachment_info)) {
                $attachment_info = Attachment_Model_Content_Temp::instance()->fetchByPK($_id);
            }
            if (empty($attachment_info)) {
                throw new Zeed_Exception('查无此文件');
            }
            $attachment_info = $attachment_info[0];
            
            /* 检查打点信息是否存在 */
            $where_dot = array('attachment_id' => $_id, 'gmt' => $gmt);
            $dot_info = Attachment_Model_Content_Dot::instance()->fetchByWhere($where_dot);
            if (empty($dot_info)) {
                throw new Zeed_Exception('查无此打点信息');
            }
            $dot_info = $dot_info[0];
            
            /* 执行删除 */
            Attachment_Model_Content_Dot::instance()->deleteByPK($dot_info['_id']);
            
            /* @todo 新增一条日志记录 */
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('删除失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
}

// End ^ LF ^ encoding
